﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;

namespace Licesing.Keys
{
    public class DriveKey
    {
        public static string GetDriveSerial()
        {
             return GetDriveSerial(null);
        }

        public static string GetDriveSerial(String DriveName)
        {
            if (DriveName == string.Empty)
            {
                //Find first drive
                foreach (DriveInfo compDrive in DriveInfo.GetDrives())
                {
                    if (compDrive.IsReady)
                    {
                        DriveName = compDrive.RootDirectory.ToString();
                        break;
                    }
                }
            }

            if (DriveName.EndsWith(":\\"))
            {
                //C:\ -> C
                DriveName = DriveName.Substring(0, DriveName.Length - 2);
            }

            ManagementObject disk = new ManagementObject(@"win32_logicaldisk.deviceid=""" + DriveName + @":""");
            disk.Get();

            string volumeSerial = disk["VolumeSerialNumber"].ToString();
            disk.Dispose();

            return volumeSerial;
        }
    }
}
